﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace lox_z12Class
{
    class LoxInstance
    {
        private LoxClass klass;
        private Dictionary<String, Object> fields = new Dictionary<String, Object>();

        public LoxInstance(LoxClass klass) { this.klass = klass; }

        public Object get(Token name)
        {
            if (fields.ContainsKey(name.lexeme)) {
                return fields[name.lexeme];
            }

            LoxFunction method = klass.findMethod(name.lexeme);
            if (method != null)
                return method.bind(this);

            throw new RuntimeError(name, // [hidden]
                                   "Undefined property '" + name.lexeme + "'.");
        }
        public void set(Token name, Object value) { fields[name.lexeme]= value; }
        override public String ToString() { return "打印类实例："+klass.name + " [instance]"; }
    }
}
